using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PipelineManager : MonoBehaviour {

    public GameObject pipelineTemplate;

    List<Pipeline> pipelines = new List<Pipeline>();

	public float speed;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	IEnumerator GeneratePipelines() {

        for (int i = 0; i < 3; i++)
        {
			if (pipelines.Count < 3)
			{
				GeneratePipeline();
			}
			else
			{
				pipelines[i].Init();
				pipelines[i].enabled = true;
			}
			yield return new WaitForSeconds(speed);
		}
	}

	void GeneratePipeline() {
        if (pipelines.Count < 3)
        {
			GameObject obj = Instantiate(pipelineTemplate, this.transform);
			Pipeline p = obj.GetComponent<Pipeline>();
			pipelines.Add(p);
		}
	}

	Coroutine coroutine = null;
	public void StartRun() {

		coroutine = StartCoroutine(GeneratePipelines());
	}

	public void Stop()
	{
		StopCoroutine(coroutine);
        for (int i = 0; i < pipelines.Count; i++)
        {
			pipelines[i].enabled = false;
        }
	}

	public void Init()
	{
		for (int i = 0; i < pipelines.Count; i++)
		{
			Destroy(pipelines[i].gameObject);
		}
		pipelines.Clear();
	}
}
